package com.xinxuan.contentcenter.base;

import static java.util.stream.Collectors.toList;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
 * @author zs
 * @Desc: 分页响应
 * @date 2021/7/1上午10:41
 */
@Data
@Builder
@AllArgsConstructor
public class PageResp<T> implements Serializable {

    private static final long serialVersionUID = 3069859389661111761L;

    /**
     * 总条数
     */
    private long total;
    /**
     * 总页数
     */
    private long pages;
    /**
     * 当前页
     */
    private long current;
    /**
     * 当前页条数
     */
    private long size;
    /**
     * 数据列表
     */
    private List<T> records;

    public PageResp(Integer current, Integer size) {
        this.current = current;
        this.size = size;
    }

    public PageResp() {}

    public PageResp<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    /**
     * IPage 的泛型转换
     *
     * @param mapper
     *            转换函数
     * @param <R>
     *            转换后的泛型
     * @return 转换泛型后的 IPage
     */
    @SuppressWarnings("unchecked")
    public <R> PageResp<R> convert(Function<? super T, ? extends R> mapper) {
        List<R> collect = this.getRecords().stream().map(mapper).collect(toList());
        return ((PageResp<R>)this).setRecords(collect);
    }
}